import { URL_LIST } from "@data/url";
import { AddVoucherCodeDialog } from "@dialogs/addVoucherCodeDialog";
import { AssignSpecificProductsDialog } from "@dialogs/assignSpecificProductsDialog";
import { RightSideDetailsPage } from "@pageElements/rightSideDetailsSection";
import { BasePage } from "@pages/basePage";
import { expect, Page } from "@playwright/test";

import { DeleteDialog } from "./dialogs/deleteDialog";

export class VouchersPage extends BasePage {
  readonly page: Page;

  readonly addVoucherCodeDialog: AddVoucherCodeDialog;

  readonly rightSideDetailsPage: RightSideDetailsPage;

  readonly deleteVoucherDialog: DeleteDialog;

  readonly assignSpecificProductsDialog: AssignSpecificProductsDialog;

  constructor(
    page: Page,
    readonly createVoucherButton = page.getByTestId("create-voucher"),
    readonly categoriesTab = page.getByTestId("categories-tab"),
    readonly collectionsTab = page.getByTestId("collections-tab"),
    readonly productsTab = page.getByTestId("products-tab"),
    readonly bulkDeleteButton = page.getByTestId("bulk-delete-button"),
    readonly deleteButton = page.getByTestId("button-bar-delete"),
    readonly addCodeButton = page.getByTestId("add-code-button"),
    readonly usageLimitCheckbox = page
      .getByTestId("has-usage-limit")
      .locator('input[type="checkbox"]'),
    readonly oncePerCustomerLimitCheckbox = page.getByTestId("apply-once-per-customer"),
    readonly onlyForStaffLimitCheckbox = page.getByTestId("only-for-staff"),
    readonly singleUseLimitCheckbox = page.getByTestId("single-use"),
    readonly usageLimitSection = page.getByTestId("usage-limit-section"),
    readonly valueSection = page.getByTestId("value-section"),
    readonly minimumRequirementsSection = page.getByTestId("minimum-requirements-section"),
    readonly usageLimitInput = page.getByTestId("usage-limit").locator("input"),
    readonly assignedSpecificProductsTable = page.getByTestId("assigned-specific-products-table"),
    readonly assignedSpecificProductRow = page.getByTestId("assigned-specific-product"),
    readonly saveButton = page.getByTestId("button-bar-confirm"),
    readonly voucherNameInput = page.getByTestId("voucher-name-input"),
    readonly discountValueInput = page.getByTestId("discount-value-input"),
    readonly minimumQuantityOfItemsInput = page
      .getByTestId("minimum-quantity-of-items-input")
      .locator("input"),
    readonly minRequirementsValueInput = page.getByTestId("price-field"),
    readonly startDateInput = page.getByTestId("start-date-input"),
    readonly endDateInput = page.getByTestId("end-date-input"),
    readonly startHourInput = page.getByTestId("start-hour-input"),
    readonly endHourInput = page.getByTestId("end-hour-input"),
    readonly setEndDateCheckbox = page.getByTestId("has-end-date"),
    readonly autoGeneratedCodesItem = page.getByTestId("auto-generate codes"),
    readonly manualGeneratedCodesItem = page.getByTestId("manual"),
    readonly percentDiscountTypeButton = page.getByTestId("VALUE_PERCENTAGE"),
    readonly freeShippingDiscountTypeButton = page.getByTestId("SHIPPING"),
    readonly minimalOrderValueButton = page.getByTestId("ORDER"),
    readonly minimumQuantityOfItemsButton = page.getByTestId("ITEM"),
    readonly specificProductsButton = page.getByTestId("SPECIFIC_PRODUCT"),
    readonly assignCollectionButton = page.getByTestId("assign-collection-button"),
    readonly assignCategoryButton = page.getByTestId("assign-category-button"),
    readonly assignProductButton = page.getByTestId("assign-products"),
    readonly assignCollectionSection = page.getByTestId("assign-collection-section"),
    readonly assignCategoriesSection = page.getByTestId("assign-category-section"),
    readonly assignProductSection = page.getByTestId("assign-product-section"),
  ) {
    super(page);
    this.page = page;
    this.addVoucherCodeDialog = new AddVoucherCodeDialog(page);
    this.rightSideDetailsPage = new RightSideDetailsPage(page);
    this.deleteVoucherDialog = new DeleteDialog(page);
    this.assignSpecificProductsDialog = new AssignSpecificProductsDialog(page);
  }

  async typeVoucherName(voucherName = "special voucher automation") {
    await this.voucherNameInput.fill(voucherName);
  }

  async typeMinimumQuantityOfItems(minimumQuantityOfItems = "3") {
    await this.minimumQuantityOfItemsInput.fill(minimumQuantityOfItems);
  }

  async typeUsageLimit(usageLimit = "10000") {
    await this.usageLimitInput.fill(usageLimit);
  }

  async clickCreateVoucherButton() {
    await this.createVoucherButton.click();
  }

  async clickAssignCollectionButton() {
    await this.assignCollectionButton.click();
  }

  async clickAssignProductButton() {
    await this.assignProductButton.click();
  }

  async clickCollectionsTab() {
    await this.collectionsTab.click();
  }

  async clickProductsTab() {
    await this.productsTab.click();
  }

  async clickAssignCategoryButton() {
    await this.assignCategoryButton.click();
  }

  async clickSpecificProductsButton() {
    await this.specificProductsButton.click();
  }

  async clickBulkDeleteButton() {
    await this.bulkDeleteButton.click();
  }

  async clickDeleteSingleVoucherButton() {
    await this.deleteButton.click();
  }

  async clickSetEndDateCheckbox() {
    await this.setEndDateCheckbox.click();
  }

  async clickMinimumQuantityOfItemsButton() {
    await this.minimumQuantityOfItemsButton.click();
  }

  async clickMinimalOrderValueButton() {
    await this.minimalOrderValueButton.click();
  }

  async clickUsageTotalLimitCheckbox() {
    await this.usageLimitCheckbox.click();
    await expect(await this.usageLimitCheckbox.isChecked()).toBeTruthy();
    await this.usageLimitInput.waitFor({ state: "attached" });
  }

  async clickSingleUseLimitCheckbox() {
    await this.singleUseLimitCheckbox.click();
  }

  async clickOnlyForStaffLimitCheckbox() {
    await this.onlyForStaffLimitCheckbox.click();
  }

  async clickOncePerCustomerLimitCheckbox() {
    await this.oncePerCustomerLimitCheckbox.click();
  }

  async clickPercentDiscountTypeButton() {
    await this.percentDiscountTypeButton.click();
  }

  async clickFreeShippingDiscountTypeButton() {
    await this.freeShippingDiscountTypeButton.click();
  }

  async clickSaveButton() {
    await this.saveButton.click();
  }

  async clickAutoGeneratedCodesItem() {
    await this.autoGeneratedCodesItem.click();
  }

  async clickManualGeneratedCodesItem() {
    await this.manualGeneratedCodesItem.click();
  }

  async clickAddCodeButton() {
    await this.addCodeButton.click();
  }

  async gotoVouchersListPage() {
    await this.page.goto(URL_LIST.vouchers);
    await this.waitForDOMToFullyLoad();
  }

  async gotoExistingVoucherPage(voucherId: string) {
    const existingVoucherUrl = `${URL_LIST.vouchers}${voucherId}`;

    console.log(`Navigating to existing variant: ${existingVoucherUrl}`);
    await this.page.goto(existingVoucherUrl);
  }

  async gotoVoucherAddPage() {
    console.log(`Navigating to voucher add page: ${URL_LIST.vouchersAddPage}`);
    await this.page.goto(URL_LIST.vouchersAddPage);
  }

  async typeDiscountValueInChannel(channel = "Channel-PLN", discountValue = "10") {
    const valueInput = await this.valueSection
      .getByTestId(channel)
      .locator(this.discountValueInput);

    await valueInput.scrollIntoViewIfNeeded();
    await valueInput.fill(discountValue);
  }

  async typeMinimumOrderValue(channel = "Channel-PLN", minimumOrderValue = "10") {
    await this.minimumRequirementsSection
      .getByTestId(channel)
      .locator(this.minRequirementsValueInput)
      .fill(minimumOrderValue);
  }
}
